<?php
/**
 * Created by PhpStorm.
 * User: divya
 * Date: 2018/8/3
 * Time: 下午12:09
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;

class OrderPlace extends BaseValidate
{
    protected $products = [
        'product_id' => 1
    ];
    protected $rule = [
        'products' => 'checkProducts'
    ];
    protected $singRule = [
        'product_id'=>'require|isPositiveInteger',
        'count'=>'require|isPositiveInteger'
    ];
    protected function checkProducts($values)
    {
        if (!is_array($values)) {
            throw new ParameterException([
                'msg' => '参数不正确'
            ]);
        }
        if (empty($values)) {
            throw new ParameterException([
                'msg' => '商品列表不能为空'
            ]);
        }
        foreach ($values as $value) {
            $this->checkProduct($value);
        }
        return true;
    }
    private function checkProduct($item){
        $validate = new BaseValidate($this->singRule);
        $result = $validate->check($item);
        if (!$result){
            throw new ParameterException([
                'msg'=>'商品列表参数错误'
            ]);
        }
    }
}